<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>音频播放列表</title>
    <style>
        #playSelect{
            width: 200px;
            height:300px;
        }
    </style>
</head>
<body>
    <div>
        <h3 id="playInfo">

        </h3>
    </div>
    <audio controls id="audio" src=""></audio>
    <div>
        <input id="prev" type="button" value="上一首">
        <input id="next" type="button" value="下一首">
    </div>
    <div>
        <select id="playSelect" multiple>

        </select>
    </div>
    <script>
        let playInfo = document.getElementById("playInfo");
        let audio = document.getElementById("audio");
        let prev = document.getElementById("prev");
        let next = document.getElementById("next");
        let playSelect = document.getElementById("playSelect");
        let playIndex = 0;
        let musicAry = [
            {name:"暗号",src:"D:/funny/Music/周杰伦/八度空间/MP3/周杰伦 - 暗号.mp3"},
            {name:"半岛铁盒",src:"D:/funny/Music/周杰伦/八度空间/MP3/周杰伦 - 半岛铁盒.mp3"},
            {name:"半兽人",src:"D:/funny/Music/周杰伦/八度空间/MP3/周杰伦 - 半兽人.mp3"}
        ];
        for(let i = 0;i < musicAry.length;i++){
            let opt = new Option(musicAry[i].name,musicAry[i].src);
            playSelect.append(opt);
            if(i == 0){
                opt.selected = true;
                audio.src = opt.value;
            }
        }
        next.onclick = function(){
            playIndex++;
            if(playIndex >= musicAry.length){
                playIndex = 0;
            }
            audio.src = musicAry[playIndex].src;
            playSelect.selectedIndex = playIndex;
            // for(let i = 0;i < playSelect.options.length;i++){
            //     if(i == playIndex){
            //         playSelect.options[i].selected = true;
            //     }else{
            //         playSelect.options[i].selected = false;
            //     }
            // }
            audio.play();
        }
        playSelect.ondblclick = function(){
            console.log(this.selectedIndex);
            playIndex = this.selectedIndex;
            audio.src = musicAry[playIndex].src;
            audio.play();
        }
        audio.onended = function(){
            playIndex++;
            if(playIndex >= musicAry.length){
                playIndex = 0;
            }
            audio.src = musicAry[playIndex].src;
            playSelect.selectedIndex = playIndex;
            audio.play();
        }
        audio.onplay = function(){
            playInfo.innerHTML = "正在播放：" + musicAry[playIndex].name;
        }
        audio.onpause = function(){
            playInfo.innerHTML = "暂停播放：" + musicAry[playIndex].name;
        }
    </script>
</body>
</html>